// 1. 引入定义的函数
import { defineStore } from 'pinia'
// 2. 引入接口函数
import { reqGetCategory1ListData, reqGetCategory2ListData, reqGetCategory3ListData } from '@/api/product/category'
import type {CategoryStateModel} from '@/api/product/model/categoryModel'
// 3. 定义store实例对象 并导出
export const useCategoryListStore = defineStore('categoryList', {
  state: ():CategoryStateModel => {
    return {
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      category1List: [],
      category2List: [],
      category3List: []
    }
  },
  actions: {
    // 所有的同步或异步操作都 在actions中来实现
    // 1. 获取一级导航分类数据
    async getCategory1ListData() {
      const result = await reqGetCategory1ListData()
      // console.log('cc',result)
      this.category1List = result;
    },
    // 2. 获取二级导航分类数据
    async getCategory2ListData(category1Id: number) {
      // const result = await reqGetCategory2ListData(category1Id)
      // console.log('cc',result)
      // this.category2List = result;
      this.category1Id = category1Id

      // 让二级分类下拉框重置或是不显示内容
      this.category2Id = undefined
      this.category3Id = undefined

      this.category3List = []

      this.category2List = await reqGetCategory2ListData(category1Id)
    },
    // 3. 获取三级导航分类数据
    async getCategory3ListData(category2Id: number) {
      // const result = await reqGetCategory3ListData(category2Id)
      // console.log('cc',result)
      // this.category3List = result;
      this.category2Id = category2Id
      this.category3Id = undefined

      this.category3List = await reqGetCategory3ListData(category2Id)
    },
    // 4. 给三级id赋值
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    }
  },
  getters: {

  }
})

// pinia中去除了mutations